/*
 *@本JS功能：排课管理-自动排课
 *@本JS作者：杨锡君
 *@编写时间：2017年11月15日
 */
$(function(){
	var Alert1 = new Alert(); 
	Alert1.init();
	var confirm1 = new Confirm();
	var loading1 = new Loading();
	loading1.init();
	var $department = $(".department");
	var $profession = $(".profession");
	var $direct = $(".direct");
	var $teacher = $(".teacher");
	var $courseName = $(".courseName");
	var $coursePlace = $(".coursePlace");
	var $courseTable = $(".courseTable");
	var $saveTable = $(".saveTable");
	var $dateStart = $(".dateStart");
	var $dateEnd = $(".dateEnd");
	var tableModel = '<tr><th></th><th><div>星期一</div></th><th><div>星期二</div></th><th><div>星期三</div></th><th><div>星期四</div></th><th><div>星期五</div></th><th><div>星期六</div></th><th><div>星期日</div></th></tr><tr><td rowspan="2"><div class="fl">上&emsp;午</div><div class="fl"><div>第一节</div><div>第二节</div></div></td><td><div data-value="0" data-num="1" data-weeklist="1" class="tableAddSection"></div></td><td><div data-value="0" data-num="1" data-weeklist="2" class="tableAddSection"></div></td><td><div data-value="0" data-num="1" data-weeklist="3" class="tableAddSection"></div></td><td><div data-value="0" data-num="1" data-weeklist="4" class="tableAddSection"></div></td><td><div data-value="0" data-num="1" data-weeklist="5" class="tableAddSection"></div></td><td><div data-value="0" data-num="1" data-weeklist="6" class="tableAddSection"></div></td><td><div data-value="0" data-num="1" data-weeklist="7" class="tableAddSection"></div></td></tr><tr><td><div data-value="0" data-num="2" data-weeklist="1" class="tableAddSection"></div></td><td><div data-value="0" data-num="2" data-weeklist="2" class="tableAddSection"></div></td><td><div data-value="0" data-num="2" data-weeklist="3" class="tableAddSection"></div></td><td><div data-value="0" data-num="2" data-weeklist="4" class="tableAddSection"></div></td><td>	<div data-value="0" data-num="2" data-weeklist="5" class="tableAddSection"></div></td><td><div data-value="0" data-num="2" data-weeklist="6" class="tableAddSection"></div></td><td><div data-value="0" data-num="2" data-weeklist="7" class="tableAddSection"></div></td></tr><tr><td rowspan="2"><div class="fl">下&emsp;午</div><div class="fl"><div>第三节</div><div>第四节</div></div></td><td><div data-value="0" data-num="3" data-weeklist="1" class="tableAddSection"></div></td><td>	<div data-value="0" data-num="3" data-weeklist="2" class="tableAddSection"></div></td><td><div data-value="0" data-num="3" data-weeklist="3" class="tableAddSection"></div></td><td><div data-value="0" data-num="3" data-weeklist="4" class="tableAddSection"></div></td><td><div data-value="0" data-num="3" data-weeklist="5" class="tableAddSection"></div></td><td><div data-value="0" data-num="3" data-weeklist="6" class="tableAddSection"></div></td><td><div data-value="0" data-num="3" data-weeklist="7" class="tableAddSection"></div></td></tr><tr><td><div data-value="0" data-num="4" data-weeklist="1" class="tableAddSection"></div></td><td><div data-value="0" data-num="4" data-weeklist="2" class="tableAddSection"></div></td><td><div data-value="0" data-num="4" data-weeklist="3" class="tableAddSection"></div></td><td><div data-value="0" data-num="4" data-weeklist="4" class="tableAddSection"></div></td><td><div data-value="0" data-num="4" data-weeklist="5" class="tableAddSection"></div></td><td><div data-value="0" data-num="4" data-weeklist="6" class="tableAddSection"></div></td><td><div data-value="0" data-num="4" data-weeklist="7" class="tableAddSection"></div></td></tr><tr><td rowspan="2"><div class="fl">晚&emsp;上</div><div class="fl"><div>第五节</div></div>	</td><td><div data-value="0" data-num="5" data-weeklist="1" class="tableAddSection"></div></td><td><div data-value="0" data-num="5" data-weeklist="2" class="tableAddSection"></div></td><td><div data-value="0" data-num="5" data-weeklist="3" class="tableAddSection"></div></td><td><div data-value="0" data-num="5" data-weeklist="4" class="tableAddSection"></div></td><td><div data-value="0" data-num="5" data-weeklist="5" class="tableAddSection"></div></td><td><div data-value="0" data-num="5" data-weeklist="6" class="tableAddSection"></div></td><td><div data-value="0" data-num="5" data-weeklist="7" class="tableAddSection"></div></td></tr>';
	var courseArrangeMana = {
		init:function(){
			this.bindEvent();
		},
		bindEvent:function(){
			var self = this;
			self.departmentList();
			self.typeTap();	
			self.saveTableBtn();
			self.dateChoose();
			self.tableEvent();
            $.ajax({
                type:"post",
                url:"skipScheduleMana",
                async:true,
                success:function(msg){
                    msg = JSON.parse(msg);
                    if(msg.paramMap.selectCourseMark == 1){
                        $(".female").show();
                    }
                    if(msg.paramMap.selectCourseMark == 2){
                        $(".female").hide();
                    }
                }
            });
		},
		tableEvent:function(){
			var self = this;
			$courseTable.html(tableModel);
			self.tableClick();
		},
		dateChoose:function(){
//			日期插件载入
			datePicker.datePickerMonth($(".timeExtend"))
		},
		saveTableBtn:function(){
//			保存手动排课
			$saveTable.off("click").click(function(){
				if($(".tableAddSection[data-value*=2]").length  == 0){
					Alert1.content = "未自动排课，请先自动排课后保存";
					Alert1.init();
					Alert1.show();
					return false;
				}else{
					var jsonStr = [];
					$.each($(".tableAddSection[data-value*=2]"), function(key) {
						jsonStr.push({"classDateEnd":$(this).attr("data-classdateend"),"classDateStart":$(this).attr("data-classdatestart"),"classNumList":[Number($(this).attr("data-num"))],"courseId":Number($(this).attr("data-courseid")),"departmentId":Number($(this).attr("data-departmentid")),"mainId":Number($(this).attr("data-mainid")),"professionId":Number($(this).attr("data-professionid")),"selectMark":Number($(".radioCheck").attr("data-value")),"siteId":Number($(this).attr("data-siteid")),"stuorteaId":$(this).attr("data-stuorteaid"),"weekList":[Number($(this).attr("data-weeklist"))]});
					});
					loading1.show();
					$.ajax({
						type:"post",
						url:"saveAutoCourseTab",
						async:true,
						data:{
							"jsonStr":JSON.stringify({"tabList":jsonStr})
						},
						success:function(msg){
							loading1.hide();
                            msg = JSON.parse(msg);
							if(msg.result == "success"){
								Alert1.content = "自动排课成功";
								Alert1.success = function(){
									var json = JSON.stringify({
										"pageName": "autoCourseArrangeMana"
									});
									window.location.href = "skipPage?jsonStr=" + json;
								};
								Alert1.init();
								Alert1.show();
							}else{
								Alert1.content = msg.failMsg;
								Alert1.init();
								Alert1.show();
							}
						}
					});
				}
			});
		},
		tableClick:function(){
//			课表点击
			var self = this;
			$(".tableAddSection").off("click").click(function(){
				var flag = self.verificationEvent();
				if(flag){
					if($(this).attr("data-value") == 0){
						$(this).attr({"data-classdatestart":$dateStart.val(),"data-classdateend":$dateEnd.val(),"data-value":"2","data-departmentid":$department.val(),"data-professionid":$profession.val(),"data-mainid":$direct.val(),"data-siteid":$coursePlace.val(),"data-stuorteaid":$teacher.val(),"data-courseid":$courseName.val()});
						$(this).css("background",$courseName.children("option:selected").attr("data-color")+" url(pc/images/ico_courseCheck.png) no-repeat right top").html("<br>"+$courseName.children("option:selected").html()+"<br><br>"+$dateStart.val()+"——"+$dateEnd.val());
					
					}else if($(this).attr("data-value") == 2){
						$(this).css("background","").html("").attr({"data-classdatestart":"","data-classdateend":"","data-courseid":"","data-value":"0","data-departmentid":"","data-professionid":"","data-mainid":"","data-siteid":"","data-stuorteaid":"","data-courseid":""});	
					}
					
				}
			});
		},
		verificationEvent:function(){
//			验证
			if($department.val() == 0){
				Alert1.content = "请选择院系";
				Alert1.init();
				Alert1.show();
				return false;
			}
			if($profession.val() == 0){
				Alert1.content = "请选择专业";
				Alert1.init();
				Alert1.show();
				return false;
			}
			if($direct.val() == 0){
				Alert1.content = "请选择专业方向";
				Alert1.init();
				Alert1.show();
				return false;
			}
			if($teacher.val() == 0){
				Alert1.content = "请选择教师";
				Alert1.init();
				Alert1.show();
				return false;
			}
			if($courseName.val() == 0){
				Alert1.content = "请选择课程";
				Alert1.init();
				Alert1.show();
				return false;
			}
			if($coursePlace.val() == 0){
				Alert1.content = "请选择上课地点";
				Alert1.init();
				Alert1.show();
				return false;
			}
			if($coursePlace.val() == 0){
				Alert1.content = "请选择上课地点";
				Alert1.init();
				Alert1.show();
				return false;
			}
			if($dateStart.val() == ""){
				Alert1.content = "请选择课程开始时间";
				Alert1.init();
				Alert1.show();
				return false;
			}
			if($dateEnd.val() == 0){
				Alert1.content = "请选择课程结束时间";
				Alert1.init();
				Alert1.show();
				return false;
			}
			if(Number($dateEnd.val().replace(/-/g,""))<Number($dateStart.val().replace(/-/g,""))){
				Alert1.content = "课程结束时间不能在课程开始时间之前";
				Alert1.init();
				Alert1.show();
				return false;
			}
			return true;
		},
		typeTap:function(){
			//排课类型选择
			var self = this;
			$(".male").off("click").on("click", function() {
				$(this).removeClass("radio").addClass("radioCheck");
				$(".female").removeClass("radioCheck").addClass("radio");
				self.selectInit();
				self.tableEvent();
			});
			$(".female").off("click").on("click", function() {
				$(this).removeClass("radio").addClass("radioCheck");
				$(".male").removeClass("radioCheck").addClass("radio");
				self.selectInit();
				self.tableEvent();
			});
		},
		selectInit:function(){
			var self = this;
			$department.html("<option value=\"0\">请选择</option>");
			$profession.html("<option value=\"0\">请选择</option>");
			$direct.html("<option value=\"0\">请选择</option>");
			$teacher.html("<option value=\"0\">请选择</option>");
			$courseName.html("<option value=\"0\">请选择</option>");
			$coursePlace.html("<option value=\"0\">请选择</option>");
			$dateStart.val("");
			$dateEnd.val("");
			self.departmentList();
		},
		departmentList:function(){
//			获取院系
			var self = this;
			$.ajax({
				type:"post",
				url:"getDepartmentListPost",
				async:true,
				success:function(msg){
                    msg = JSON.parse(msg);
					$department.empty();
					$department.append("<option value=\"0\">请选择</option>");
					if(msg.departmentList.length!=0){
						$.map(msg.departmentList, function(opt) {
							$department.append("<option value="+ opt.departmentId +">"+ opt.departmentName +"</option>");
						});
						self.departmentChange();
					}
				}
			});
		},
		departmentChange:function(){
//			院系专业联动
			var self = this;
			$department.off("change").change(function(){
				if($(this).val() == 0){
					$profession.html("<option value=\"0\">请选择</option>");
					$direct.html("<option value=\"0\">请选择</option>");
					$teacher.html("<option value=\"0\">请选择</option>");
					$courseName.html("<option value=\"0\">请选择</option>");
					$coursePlace.html("<option value=\"0\">请选择</option>");
					self.tableEvent();
				}else{
					var jsonStr = JSON.stringify({
						"departmentId":Number($(this).val())
					});
					$.ajax({
						type:"post",
						url:"getProfessionListByDepmentId",
						async:true,
						data:{
							"jsonStr":jsonStr
						},
						success:function(msg){
                            msg = JSON.parse(msg);
                            $profession.empty();
                            $profession.append("<option value=\"0\">请选择</option>");
							if(msg.professionList.length!=0){
								$.map(msg.professionList, function(opt) {
									$profession.append("<option value="+ opt.professionId +">"+ opt.professionName +"</option>");
								});
								self.professionChange();
							}
							
						}
					});
				}	
			});
		},
		professionChange:function(){
			var self = this;
//			专业联动专业方向与教师,课程及地点
			$profession.off("change").change(function(){
				if($(this).val() == 0){
					$direct.html("<option value=\"0\">请选择</option>");
					$teacher.html("<option value=\"0\">请选择</option>");
					$courseName.html("<option value=\"0\">请选择</option>");
					$coursePlace.html("<option value=\"0\">请选择</option>");
					self.tableEvent();
				}else{
					var jsonStr = JSON.stringify({
						"professionId":Number($(this).val())
					});
	//				专业方向
					$.ajax({
						type:"post",
						url:"getMyProCourseList",
						async:true,
						data:{
							"jsonStr":jsonStr
						},
						success:function(msg){
                            msg = JSON.parse(msg);
	//						专业方向
                            $direct.empty();
                            $direct.append("<option value=\"0\">请选择</option>");
							if(msg.directList.length!=0){
								$.map(msg.directList, function(opt) {
									$direct.append("<option value="+ opt.mainId +">"+ opt.directName +"</option>");
								});
							}
							//				教师
                            $teacher.empty();
                            $teacher.append("<option value=\"0\">请选择</option>");
							if(msg.userList.length!=0){
								$.map(msg.userList, function(opt) {
									$teacher.append("<option value="+ opt.stuorteaId +">"+ opt.userName +"</option>");
								});
								//判断排课类型 得出课程列表
								if($(".radioCheck").attr("data-sex") == "m"){
									self.teacherChange();
								}else{
                                    $courseName.empty();
                                    $courseName.append("<option value=\"0\">请选择</option>");
									if(msg.courseListAll.length!=0){
										$.map(msg.courseListAll, function(opt) {
											$courseName.append("<option data-courseTitle="+ opt.courseTitle +"	data-color="+ opt.courseColer +" value="+ opt.courseId +">"+ opt.courseName +"</option>");
										});
									}
								}
								
							}
                            $coursePlace.empty();
                            $coursePlace.append("<option value=\"0\">请选择</option>");
							if(msg.siteList.length!=0){
								//						地点
								$.map(msg.siteList, function(opt) {
									$coursePlace.append("<option value="+ opt.siteId +">"+ opt.siteName +"</option>");
								});
							}
						}
					});
				}

			});
		},
		teacherChange:function(){
			//教师联动课程
			var self = this;
			$teacher.off("change").change(function(){
                if($(".radioCheck").attr("data-sex") == "m"){
                    if($(this).val() == 0){
                        $courseName.html("<option value=\"0\">请选择</option>");
                    }else{
                        var jsonStr = JSON.stringify({
                            "stuorteaId":$(this).val(),
                            "couSelectMark":1
                        });
                        $.ajax({
                            type:"post",
                            url:"getMyClasses",
                            async:true,
                            data:{
                                "jsonStr":jsonStr
                            },
                            success:function(msg){
                                msg = JSON.parse(msg);
                                $courseName.empty();
                                $courseName.append("<option value=\"0\">请选择</option>");
                                if(msg.courseList.length!=0){
                                    $.map(msg.courseList, function(opt) {
                                        $courseName.append("<option data-courseTitle="+ opt.courseTitle +"	data-color="+ opt.courseColer +" value="+ opt.courseId +">"+ opt.courseName +"</option>");
                                    });
                                }
                            }
                        });
                    }
				}

				
			});
		}
	};
	courseArrangeMana.init();
});
